package com.lxj.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxj.mybatisplus.model.User;
import com.lxj.mybatisplus.mapper.UserMapper;
import com.lxj.mybatisplus.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author 李学江
 * @since 2020-12-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public IPage<User> findAll(User user) {
        /**
         * 这里的查找参数是一个model，所以在queryWrapper里面封装数据时候 ，前端有几个查询的条件  到这里就需要往
         * queryWrapper里面添加几个参数值，（判断条件+列名+值）的方式
         */
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.hasLength(user.getUsername()), User::getUsername, user.getUsername())
                .like(StringUtils.hasLength(user.getEmail()),User::getEmail, user.getEmail());
        IPage<User> page = new Page<>(1,10);
        IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
        return iPage;
    }

    @Override
    public Integer add(User user) {
        return userMapper.insert(user);
    }
}
